﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using HadronData;

using Hadron.SpecialEffects;

namespace Hadron.Entities
{
    class Scientist
        : LandEntity
    {
        private static Rectangle[] s_rectSource;
        private static Vector2 s_vecOrigin, s_vecOriginHelp;

        private double m_dTime;
        private bool m_bWalkingForward;
        private float m_fEdgePosition;
        private bool m_bHelp;
        private Edge m_edgeGeometry;

        public Scientist(EntityMarker p_entityMarker, Environment p_environment)
            : base(p_entityMarker, p_environment)
        {
            if (s_rectSource == null)
            {
                s_rectSource = new Rectangle[2];
                s_rectSource[0] = new Rectangle(0, 0, 16, 16);
                s_rectSource[1] = new Rectangle(16, 0, 16, 16);
                s_vecOrigin = new Vector2(8.0f, 16.0f);
                s_vecOriginHelp = new Vector2(16.0f, 8.0f);
            }

            if (m_edgeAnchor == null)
                return;

            m_fEdgePosition = m_edgeAnchor.Length * (float) s_random.NextDouble();
            m_bWalkingForward = true;

            m_edgeGeometry = new Edge();
            m_circleBounds.Radius = 16.0f;

            m_nPoints = 500;
        }

        public override void Update(GameTime p_gameTime)
        {
            m_dTime = p_gameTime.TotalGameTime.TotalSeconds;

            if (m_edgeAnchor == null)
                return;

            float fDeltaTime = (float) p_gameTime.ElapsedGameTime.TotalSeconds;
            float fEdgeLength = m_edgeAnchor.Length;

            float fWalkSpeed = 16.0f + (float)s_random.NextDouble() * 4.0f;
            if (m_bWalkingForward)
            {
                m_fEdgePosition += fDeltaTime * fWalkSpeed;
                if (m_fEdgePosition > fEdgeLength)
                    m_bWalkingForward = false;
            }
            else
            {
                m_fEdgePosition -= fDeltaTime * fWalkSpeed;
                if (m_fEdgePosition < 0.0f)
                    m_bWalkingForward = true;
            }

            Edge edgeAnchorWorld = new Edge();
            edgeAnchorWorld.Transform(m_layerAnchor.Placement, m_edgeAnchor);
            Position = edgeAnchorWorld.Start + edgeAnchorWorld.Direction * m_fEdgePosition;
            m_trnPlacement.RotationDirection = edgeAnchorWorld.Direction;

            m_circleBounds.Centre = Position;

            m_edgeGeometry.Start = Vector2.Zero;
            m_edgeGeometry.End = Vector2.UnitY * -16.0f;
            m_edgeGeometry.Transform(m_trnPlacement);
        }

        public override void Draw(SpriteBatch p_spriteBatch)
        {
            // scientist sprite frame
            int nFrame = ((int)(m_dTime * 4.0)) % 2;
            p_spriteBatch.Draw(AssetManager.Textures.Scientist, Position, s_rectSource[nFrame], Color.White, 
                Orientation, s_vecOrigin, 1.0f,
                m_bWalkingForward ? SpriteEffects.None : SpriteEffects.FlipHorizontally, 0.0f);

            // call for help
            float fOpacity = (float) (m_dTime % 3.0);
            if (fOpacity < 1.0f)
                fOpacity = 0.0f;
            else if (fOpacity < 1.5f)
            {
                fOpacity = (fOpacity - 1.0f) * 2.0f;
                if (!m_bHelp)
                {
                    m_bHelp = true;
                    AssetManager.Sounds.ScientistHelp.Play(m_environment.ComputeSoundVolume(Position));
                }
            }
            else if (fOpacity < 2.0f)
                fOpacity = 1.0f;
            else
            {
                fOpacity = (3.0f - fOpacity) * 2.0f;
                m_bHelp = false;
            }
            p_spriteBatch.Draw(AssetManager.Textures.ScientistHelp, Position - Vector2.UnitY * 24.0f, null,
                new Color(Color.White, fOpacity), 0.0f, s_vecOriginHelp, 1.0f, SpriteEffects.None, 0.0f);
        }

        public override EntityType Type
        {
            get { return EntityType.Scientist; }
        }

        public override bool Hostile
        {
            get { return false; }
        }

        public override bool Destructuble
        {
            get { return false; }
        }

        public override Geometry Geometry
        {
            get { return m_edgeGeometry; }
        }

        public override void OnPlayerHit()
        {
            // handle boarding on ship
            PlayerShip playerShip = m_environment.PlayerShip;
            if (!playerShip.Landed)
                return;

            // stats
            ++playerShip.ScientistsRescued;
            m_environment.Context.Score += m_nPoints;

            // effect
            m_environment.SpecialEffects.Add(new RescuedScientist(Position - Vector2.UnitX * 16.0f));
            m_entityStatus = EntityStatus.Deactivated;
            AssetManager.Sounds.ScientistThanks.Play();
        }
    }
}
